import axios,{AxiosHeaders} from "axios";
import { message } from "antd";
import store from '../redux'
axios.defaults.baseURL = 'http://119.3.122.213:9090/dome/web_admin/#';
// http://119.3.122.213:9090/dome/web_admin/#/login
axios.defaults.timeout = 60000;
// 请求拦截
axios.interceptors.request.use((config:any)=>{
    // 设置请求头
    // let token = localStorage.getItem("token");
    let token = store.getState().userReducer.token;
    config.headers.Authorization = token
    return config
})
//  响应拦截
axios.interceptors.response.use((res:any)=>{
     if(res.data.meta.status === 401 || res.data.meta.status === 400){
        window.location.href = '#/login'
        message.error(res.data.meta.msg)
        return 
     }
     if(res.data.meta.status !==200 && res.data.meta.status !==201 && res.data.meta.status !== 204){
        message.error(res.data.meta.msg)
     }
     return res;
})
let http = (url:string,method:string,data:DataParams={},headers?:AxiosHeaders):Promise<any>=>{
    return axios({
        url,
        method,
        params:method==='get'?data:null,
        data:method!=="get"?data:null,
        headers
    })
}

export default http;
